package com.konsung.ui.defineview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jwenfeng.library.pulltorefresh.view.FooterView;
import com.konsung.healthfile.R;
import com.konsung.util.UiUtils;

/**
 * 上拉加载更多布局
 */
public class LoadMoreView extends FrameLayout implements FooterView {

    private TextView tv; //文本显示内容
    private ImageView arrow; //图标显示
    private ProgressBar progressBar; //加载进度圈

    /**
     * 构造器
     * @param context 上下文
     */
    public LoadMoreView(Context context) {
        this(context, null);
    }

    /**
     * 构造器
     * @param context 上下文
     * @param attrs attrs
     */
    public LoadMoreView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * 构造器
     * @param context 上下文
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public LoadMoreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化
     * @param context 上下文
     */
    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_header, null);
        addView(view);
        tv = (TextView) view.findViewById(R.id.header_tv);
        arrow = (ImageView) view.findViewById(R.id.header_arrow);
        progressBar = (ProgressBar) view.findViewById(R.id.header_progress);
    }

    @Override
    public void begin() {

    }

    @Override
    public void progress(float progress, float all) {
        float s = progress / all;
        if (s >= 0.9f) {
            arrow.setRotation(0);
        } else {
            arrow.setRotation(180);
        }
        if (progress >= all - 10) {
            tv.setText(UiUtils.getString(R.string.loosen_load));
        } else {
            tv.setText(UiUtils.getString(R.string.pull_load));
        }
    }

    @Override
    public void finishing(float progress, float all) {

    }

    @Override
    public void loading() {
        arrow.setVisibility(GONE);
        progressBar.setVisibility(VISIBLE);
        tv.setText(UiUtils.getString(R.string.loading1));
    }

    @Override
    public void normal() {
        arrow.setVisibility(VISIBLE);
        progressBar.setVisibility(GONE);
        tv.setText(UiUtils.getString(R.string.pull_load));
    }

    @Override
    public View getView() {
        return this;
    }
}
